<template>
  <div class="app-container">
    <el-card class="search-card" shadow="hover">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-form-item label="问题中文" prop="contentZh">
          <el-input
            v-model="queryParams.contentZh"
            placeholder="请输入推荐问题中文"
            clearable
            prefix-icon="el-icon-search"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="问题英文" prop="contentEn">
          <el-input
            v-model="queryParams.contentEn"
            placeholder="请输入推荐问题英文"
            clearable
            prefix-icon="el-icon-search"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="hover" class="action-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['suggest:question:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['suggest:question:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['suggest:question:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['suggest:question:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-card>

    <el-card shadow="hover" class="table-card">
      <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange" border stripe>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" width="80" />
        <el-table-column label="推荐问题（中文）" align="left" prop="contentZh" min-width="200" :show-overflow-tooltip="true" />
        <el-table-column label="推荐问题（英文）" align="left" prop="contentEn" min-width="200" :show-overflow-tooltip="true" />
        <el-table-column label="排序" align="center" prop="sort" width="80" />
        <el-table-column label="状态" align="center" prop="status" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-value="0"
              inactive-value="1"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="handleStatusChange(scope.row)"
              v-hasPermi="['suggest:question:edit']"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['suggest:question:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['suggest:question:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改推荐问题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body custom-class="suggest-question-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="推荐问题（中文）" prop="contentZh">
          <el-input v-model="form.contentZh" placeholder="请输入推荐问题中文" />
        </el-form-item>
        <el-form-item label="推荐问题（英文）" prop="contentEn">
          <el-input v-model="form.contentEn" placeholder="请输入推荐问题英文" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="form.sort" :min="0" :max="999" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuestion, getQuestion, delQuestion, addQuestion, updateQuestion } from "@/api/suggest/question";

export default {
  name: "Question",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 推荐问题表格数据
      questionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contentZh: null,
        contentEn: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contentZh: [
          { required: true, message: "推荐问题中文不能为空", trigger: "blur" }
        ],
        contentEn: [
          { required: true, message: "推荐问题英文不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询推荐问题列表 */
    getList() {
      this.loading = true;
      listQuestion(this.queryParams).then(response => {
        // 确保每条记录的status都是字符串类型
        if (response.rows && response.rows.length > 0) {
          response.rows.forEach(item => {
            if (item.status !== undefined) {
              item.status = item.status.toString();
            }
          });
        }
        this.questionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contentZh: null,
        contentEn: null,
        sort: 0,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加推荐问题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQuestion(id).then(response => {
        this.form = response.data;
        // 确保status字段是字符串类型
        if (this.form.status !== undefined) {
          this.form.status = this.form.status.toString();
        }
        this.open = true;
        this.title = "修改推荐问题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 确保状态是字符串类型
          if (this.form.status !== undefined) {
            this.form.status = this.form.status.toString();
          }
          
          if (this.form.id != null) {
            updateQuestion(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuestion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除推荐问题编号为"' + ids + '"的数据项？').then(function() {
        return delQuestion(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('suggest/question/export', {
        ...this.queryParams
      }, `question_${new Date().getTime()}.xlsx`)
    },
    // 状态修改
    handleStatusChange(row) {
      // 确保status是字符串类型
      row.status = row.status.toString();
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.contentZh + '"吗？').then(function() {
        return updateQuestion(row);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);

  .search-card {
    margin-bottom: 20px;
    
    ::v-deep .el-card__body {
      padding: 15px;
    }
    
    .el-form-item {
      margin-bottom: 0;
      margin-right: 15px;
    }
    
    .el-input {
      width: 220px;
    }
    
    .el-select {
      width: 220px;
    }
    
    .el-button {
      padding: 8px 15px;
      margin-right: 10px;
      border-radius: 4px;
      
      &[type="primary"] {
        background-color: #1890ff;
        border-color: #1890ff;
        
        &:hover, &:focus {
          background-color: #40a9ff;
          border-color: #40a9ff;
        }
      }
    }
  }

  .action-card {
    margin-bottom: 20px;
    
    ::v-deep .el-card__body {
      padding: 10px 15px;
    }
  }

  .table-card {
    ::v-deep .el-card__body {
      padding: 0;
      
      .el-table {
        margin-top: 0;
      }
      
      .pagination-container {
        margin-top: 0;
        padding: 15px;
        background-color: #f5f7fa;
      }
    }
  }

  .el-form {
    margin-bottom: 20px;
  }

  .el-table {
    margin-top: 15px;
    border-radius: 6px;
    overflow: hidden;
    
    ::v-deep .el-table__header-wrapper {
      th {
        background-color: #f5f7fa;
        font-weight: bold;
      }
    }
    
    ::v-deep .el-table__row {
      transition: all 0.3s;
      
      &:hover {
        background-color: rgba(64, 158, 255, 0.08);
      }
    }
  }
  
  .mb8 {
    margin-bottom: 15px;
    
    .el-button {
      margin-right: 8px;
      font-weight: 500;
      border-radius: 4px;
      
      &[type="primary"] {
        background-color: #1890ff;
        border-color: #1890ff;
        
        &:hover, &:focus {
          background-color: #40a9ff;
          border-color: #40a9ff;
        }
      }
    }
  }
}

.suggest-question-dialog {
  ::v-deep .el-dialog__header {
    padding: 20px;
    background-color: #f5f7fa;
    border-bottom: 1px solid #e8e8e8;
    
    .el-dialog__title {
      font-size: 18px;
      font-weight: 600;
    }
  }
  
  ::v-deep .el-dialog__body {
    padding: 25px 25px 10px;
  }
  
  ::v-deep .el-input__inner {
    border-radius: 4px;
  }
  
  ::v-deep .el-form-item__label {
    font-weight: 500;
  }
  
  ::v-deep .dialog-footer {
    padding: 10px 20px 20px;
    text-align: right;
    
    .el-button {
      padding: 9px 20px;
      font-weight: 500;
      border-radius: 4px;
    }
  }
}
</style> 